package com.projectzqjz.parttimework.net;

import android.util.Log;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.TimeUnit;

import okhttp3.ConnectionPool;
import okhttp3.Cookie;
import okhttp3.CookieJar;
import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;

public class NoValidationTask {

    private NoValidationTask() {
    }

    //获取任务实例
    public static ApiService getApiService() {
        return getRetrofit().create(ApiService.class);
    }

    private static Retrofit getRetrofit() {
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        //显示日志
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor(
                new HttpLoggingInterceptor.Logger() {
                    @Override
                    public void log(String message) {
                        Log.w("IM-HTTP", "请求路径：" + message);
                    }
                });
        loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        builder.addInterceptor(loggingInterceptor);

        //公共请求头
        builder.addInterceptor(new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                Request request = chain.request()
                        .newBuilder()
                        .addHeader("Content-Type", "application/json;charset=UTF-8")
                        .addHeader("clientType", "android")
                        .build();
                return chain.proceed(request);
            }
        });
        //读写超时时间
        builder.readTimeout(5, TimeUnit.MINUTES)
                .writeTimeout(5, TimeUnit.MINUTES)
                .connectTimeout(5, TimeUnit.MINUTES);
        builder.connectionPool(new ConnectionPool(5, 1, TimeUnit.SECONDS));

//        cookies设置
        builder.cookieJar(new CookieJar() {
            private final HashMap<HttpUrl, List<Cookie>> cookiesStore = new HashMap<>(16);

            @Override
            public void saveFromResponse(HttpUrl url, List<Cookie> cookies) {
                cookiesStore.put(HttpUrl.parse(url.host()), cookies);
            }

            @Override
            public List<Cookie> loadForRequest(HttpUrl url) {
                List<Cookie> cookies = cookiesStore.get(url.host());
                return cookies != null ? cookies : new ArrayList<Cookie>();
            }
        });

        //封装
        return new Retrofit.Builder()
                .baseUrl(Url.BaseUrl)
                .client(builder.build())
                .addConverterFactory(ScalarsConverterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
}
